package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.exception.ExceptionEnum;
import org.footballmanager.pandafootball.common.exception.MyException;
import org.footballmanager.pandafootball.system.domain.Photo;
import org.footballmanager.pandafootball.system.domain.enums.FL;
import org.footballmanager.pandafootball.system.form.impl.PhotoSearchForm;
import org.footballmanager.pandafootball.system.service.PhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Controller
@SessionAttributes("photoSearchForm")
@RequestMapping("/foot/photo")
public class PhotoController extends BaseController {
    /* 实体名称 */
    protected static final String ENTITYNAME = "photo";
    protected static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.FOOT, ENTITYNAME);
    protected static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    protected static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    protected static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    protected static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);

    @Autowired
    private PhotoService photoService;
    private List<String> files = new ArrayList<>();

    /**
     * 初始化
     *
     * @param map             参数
     * @param photoSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute PhotoSearchForm photoSearchForm) {
        PhotoSearchForm newPhotoSearchForm = new PhotoSearchForm();
        if (photoSearchForm != null) {
            newPhotoSearchForm = photoSearchForm;
        }
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newPhotoSearchForm);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute PhotoSearchForm photoSearchForm) {
        photoService.search(ENTITYNAME, map, photoSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        photoService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute PhotoSearchForm photoSearchForm) {
        photoService.deleteAll(photoSearchForm, request);
        return REDIRECT;

    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map) {
        map.addAttribute(ENTITYNAME, new Photo());
        return ADDPAGE;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String save(@Valid @ModelAttribute Photo photo, BindingResult bindingResult,
                       HttpServletRequest request, @RequestParam MultipartFile img, SessionStatus s) {
        if (photo.getImg() == null || photo.getImg().length == 0) {
            bindingResult.rejectValue("img", Constants.MISFORMAT, "请选择上传图片");
        }
        if (!bindingResult.hasErrors()) {
            photoService.save(photo, img, request);
            s.setComplete();
            return REDIRECT;
        } else
            return ADDPAGE;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, photoService.get(id));
        return EDITPAGE;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public String update(@Valid @ModelAttribute Photo photo, BindingResult bindingResult,
                         HttpServletRequest request, @RequestParam MultipartFile img, SessionStatus s) {
        if (!bindingResult.hasErrors()) {
            photoService.update(photo, img, request);
            s.setComplete();
            return REDIRECT;
        } else {
            return EDITPAGE;
        }
    }

    @RequestMapping(value = "/view", method = RequestMethod.POST)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute PhotoSearchForm photoSearchForm) {
        map.addAttribute(ENTITYNAME, photoService.get(id));
        return VIEWPAGE;
    }

    @RequestMapping(value = "/more")
    public String more(Model map) {
        Photo photo = new Photo();
        photo.setType(FL.GAMER);
        map.addAttribute(ENTITYNAME, photo);
        return "foot/photo/Addmore";
    }

    @RequestMapping(value = "/saveMore", method = RequestMethod.GET)
    public String saveMore(HttpServletRequest request, @RequestParam FL fl) {
        try {
            photoService.saveMore(fl, files, request);
        } catch (UnsupportedEncodingException e) {
            throw new MyException(ExceptionEnum.CODEEXCEPTION, e);
        }
        return REDIRECT;
    }

    @RequestMapping(value = "/addMore")
    @ResponseBody
    public String addMore(@RequestParam("file") MultipartFile file, HttpServletRequest request, Photo photo) {
        photoService.addMore(file, photo.getType(), files, request);
        return Constants.OK;
    }

    @RequestMapping(value = "/find")
    @ResponseBody
    public Photo find(@RequestParam("id") Long id, @RequestParam("index") Integer index,
                      @ModelAttribute PhotoSearchForm photoSearchForm) {
        return photoService.find(photoSearchForm, id, index);
    }
}
